/*
 * copyright (c) 2009 Alexandr [Sn@ble] Surnin
 *
 * This file is part of CVP.
 *
 * CVP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * CVP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can read the full version of GNU LGPL in
 * http://www.gnu.org/copyleft/lesser.html
 */


#include "frameseeker.h"


FrameSeeker::FrameSeeker(QWidget *parent)
    : QMainWindow(parent) {

    mainLayout = new QGridLayout();

    QWidget *widget = new QWidget();
    setCentralWidget(widget);
    widget->setLayout(mainLayout);

    tracks = new TracksWidget(this);
    mainLayout->addWidget(tracks, 1, 1, 2, 3, Qt::AlignRight | Qt::AlignTop);

    player = new VideoPlayer(this);
    mainLayout->addWidget(player, 3, 3, 1, 1, Qt::AlignBottom | Qt::AlignRight);

    fileInfo = new FileInfo(this);
    mainLayout->addWidget(fileInfo, 3, 1, 1, 1, Qt::AlignBottom | Qt::AlignLeft);

    createActions();
    createMenus();

    statusBar()->showMessage("Starting...", 3000);

}


FrameSeeker::~FrameSeeker() {

    if (tracks) delete tracks;
}


void FrameSeeker::createMenus() {

    fileMenu = menuBar()->addMenu("&File");
    fileMenu->addAction(newAction);
    fileMenu->addAction(openAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    helpMenu = menuBar()->addMenu("&Help");
    helpMenu->addAction(helpAction);
    helpMenu->addAction(aboutAction);
    // TODO
}


void FrameSeeker::createActions() {

    newAction = new QAction("&New", this);
    newAction->setStatusTip("Create a new file");
    newAction->setShortcut(QKeySequence::New);
    connect(newAction, SIGNAL(triggered()),
            this, SLOT(newFile()));

    openAction = new QAction("&Open", this);
    openAction->setStatusTip("Open a video file");
    openAction->setShortcut(QKeySequence::Open);
    connect(openAction, SIGNAL(triggered()),
            this, SLOT(openFile()));

    exitAction = new QAction("&Close", this);
    exitAction->setStatusTip("Close the appliction");
    exitAction->setShortcut(QKeySequence::Close);
    connect(exitAction, SIGNAL(triggered()),
            this, SLOT(closeApp()));

    helpAction = new QAction("&Help", this);
    helpAction->setStatusTip("Get help ^_^");
    //helpAction->setShortcut();
    connect(helpAction, SIGNAL(triggered()),
            this, SLOT(help()));

    aboutAction = new QAction("&About", this);
    aboutAction->setStatusTip("About developers");
    //aboutAction->setShortcut();
    connect(aboutAction, SIGNAL(triggered()),
            this, SLOT(about()));

    // TODO
}


void FrameSeeker::newFile() {

}


void FrameSeeker::openFile() {

    QFileDialog *fileDialog = new QFileDialog(this);
    fileDialog->setFilter("Video files (*.avi *.mpg *.mpeg *.mp4 *.mkv);;"
                          "All files (*.*)");

    fileDialog->exec();
}


void FrameSeeker::closeApp() {

    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");
    msgBox.setInformativeText("Do you want to save your changes?");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    msgBox.setIcon(QMessageBox::Question);
    int ret = msgBox.exec();

    switch (ret) {
    case QMessageBox::Save:
        // Save something...
        close();
        break;
    case QMessageBox::Discard:
        close();
        break;
    case QMessageBox::Cancel:
        // Cancel was clicked
        break;
    default:
        // should never be reached
        break;
    }

}


void FrameSeeker::help() {

    QMessageBox::about(this, "HELP", "Sorry, but help is not done yet... \n Do you want help ^_^");
}


void FrameSeeker::about() {

    QMessageBox::about(this, "About Developers",
                       "<body> Main developer: <BR> "
                       "<B>Alexandr [Sn@ble] Surnin</B> <BR>"
                       "GUKiT iPhone Labs <BR>"
                       "2009 </body>");
}
